package com.dz.syndata.utils;

import com.dz.syndata.domain.server.Sys;
import org.apache.commons.lang3.time.DateFormatUtils;

import java.lang.management.ManagementFactory;
import java.text.Format;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 时间工具类
 *
 * @author ruoyi
 */
public class DateUtils extends org.apache.commons.lang3.time.DateUtils {
    public static String YYYY = "yyyy";

    public static String YYYY_MM = "yyyy-MM";

    public static String YYYY_MM_DD = "yyyy-MM-dd";

    public static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";

    public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";

    private static String[] parsePatterns = {
            "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM",
            "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM",
            "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"};

    /**
     * 获取当前Date型日期
     *
     * @return Date() 当前日期
     */
    public static Date getNowDate() {
        return new Date();
    }

    /**
     * 获取当前日期, 默认格式为yyyy-MM-dd
     *
     * @return String
     */
    public static String getDate() {
        return dateTimeNow(YYYY_MM_DD);
    }

    public static final String getTime() {
        return dateTimeNow(YYYY_MM_DD_HH_MM_SS);
    }

    public static final String dateTimeNow() {
        return dateTimeNow(YYYYMMDDHHMMSS);
    }

    public static final String dateTimeNow(final String format) {
        return parseDateToStr(format, new Date());
    }

    public static final String dateTime(final Date date) {
        return parseDateToStr(YYYY_MM_DD, date);
    }

    public static final String parseDateToStr(final String format, final Date date) {
        return new SimpleDateFormat(format).format(date);
    }

    public static final Date dateTime(final String format, final String ts) {
        try {
            return new SimpleDateFormat(format).parse(ts);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 日期路径 即年/月/日 如2018/08/08
     */
    public static final String datePath() {
        Date now = new Date();
        return DateFormatUtils.format(now, "yyyy/MM/dd");
    }

    /**
     * 日期路径 即年/月/日 如20180808
     */
    public static final String dateTime() {
        Date now = new Date();
        return DateFormatUtils.format(now, "yyyyMMdd");
    }

    /**
     * 日期型字符串转化为日期 格式
     */
    public static Date parseDate(Object str) {
        if (str == null) {
            return null;
        }
        try {
            return parseDate(str.toString(), parsePatterns);
        } catch (ParseException e) {
            return null;
        }
    }

    public static Date strToDateLong(String strDate) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        ParsePosition pos = new ParsePosition(0);
        Date strtodate = formatter.parse(strDate, pos);
        return strtodate;
    }
    public static Date strToDate(String strDate) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        ParsePosition pos = new ParsePosition(0);
        Date strtodate = formatter.parse(strDate, pos);
        return strtodate;
    }
    public static String getStringTime(String time) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        String dateString = null;
        try {
            dateString = formatter.format(formatter.parse(time));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return dateString;

    }
    public static String getStringDD(String time) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");

        String dateString = null;
        try {
            dateString = formatter.format(formatter.parse(time));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return dateString;

    }
    public static String getStringYYYY_MM_DD(String time) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH");

        String dateString = null;
        try {
            dateString = formatter.format(formatter.parse(time));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return dateString;

    }

    /**
     * @param startTime
     * @param endTime
     * @return 返回整数表示开始时间大于结束时间，反之亦然
     */
    public static Integer  compareTo(String startTime,String endTime){
        return  startTime.compareTo(endTime);
    }
    /**
     * 获取服务器启动时间
     */
    public static Date getServerStartDate() {
        long time = ManagementFactory.getRuntimeMXBean().getStartTime();
        return new Date(time);
    }

    /**
     * 计算两个时间差
     */
    public static String getDatePoor(Date endDate, Date nowDate) {
        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        long nm = 1000 * 60;
        // long ns = 1000;
        // 获得两个时间的毫秒时间差异
        long diff = endDate.getTime() - nowDate.getTime();
        // 计算差多少天
        long day = diff / nd;
        // 计算差多少小时
        long hour = diff % nd / nh;
        // 计算差多少分钟
        long min = diff % nd % nh / nm;
        // 计算差多少秒//输出结果
        // long sec = diff % nd % nh % nm / ns;
        return day + "天" + hour + "小时" + min + "分钟";
    }

    /**
     * 计算天数
     *
     * @param endDate
     * @param nowDate
     * @return
     */
    public static long getDatePoorDay(Date endDate, Date nowDate) {
        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        long nm = 1000 * 60;
        // long ns = 1000;
        // 获得两个时间的毫秒时间差异
        long diff = endDate.getTime() - nowDate.getTime();
        // 计算差多少天
        long day = diff / nd;

        return day;
    }

    /**
     * 日期天数累加
     * @param date
     * @param i
     * @return
     */
    public  static  String addDay(String date,int i){
        Format f = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date sDate = null;
        try {
            sDate = sdf.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        Calendar c1 = Calendar.getInstance();
        c1.setTime(sDate);
        c1.add(Calendar.DAY_OF_MONTH, i);  //利用Calendar 实现 Date日期+1天
        Date time = c1.getTime();
        System.out.println("Date结束日期+1 " +f.format(time));
        return  f.format(time);
    }
    /**
     * 小时
     *
     * @param endDate
     * @param nowDate
     * @return
     */
    public static long getDatePoorHour(Date endDate, Date nowDate) {
        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        long nm = 1000 * 60;
        // long ns = 1000;
        // 获得两个时间的毫秒时间差异
        long diff = endDate.getTime() - nowDate.getTime();
        // 计算差多少天
        long day = diff / nd;
        // 计算差多少小时
        long hour = diff % nd / nh;
        // 计算差多少分钟
        long min = diff % nd % nh / nm;
        // 计算差多少秒//输出结果
        // long sec = diff % nd % nh % nm / ns;
        return hour;
    }

    /**
     * 分钟
     *
     * @param endDate
     * @param nowDate
     * @return
     */
    public static long getDatePoorMin(Date endDate, Date nowDate) {
        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        long nm = 1000 * 60;
        // long ns = 1000;
        // 获得两个时间的毫秒时间差异
        long diff = endDate.getTime() - nowDate.getTime();
        // 计算差多少天
        long day = diff / nd;
        // 计算差多少小时
        long hour = diff % nd / nh;
        // 计算差多少分钟
        long min = diff % nd % nh / nm;
        // 计算差多少秒//输出结果
        // long sec = diff % nd % nh % nm / ns;
        return min;
    }
    public  static  String yesterdayDate(){
        //实例化当天的日期
        Date today = new Date();
        //用当天的日期减去昨天的日期
        Date yesterdayDate = new Date(today.getTime()-86400000L);
        String yesterday = new SimpleDateFormat("yyyy-MM-dd").format(yesterdayDate);
        System.out.println(yesterday);
        return yesterday;
    }
    public  static  String yesterdayDate(String dateTime){
        //将日期转换为Date
        Date date = strToDate(dateTime);

        //用当天的日期减去昨天的日期
        Date yesterdayDate = new Date(date.getTime()-86400000L);
        String yesterday = new SimpleDateFormat("yyyy-MM-dd").format(yesterdayDate);
        System.out.println(yesterday);
        return yesterday;
    }


    public static void main(String[] args) {
//        String stringTime = DateUtils.getStringTime(DateUtils.yesterdayDate() + " 23:59:00");
//        System.out.println(stringTime);
//        System.out.println(getDate());
//        System.out.println(yesterdayDate());
//        Integer integer = DateUtils.compareTo("2021-09-11", "2021-08-13");
//        String s = yesterdayDate("2021-11-03");
//        System.out.println("in"+integer);
//        System.out.println("day:"+s);
//        if (selectDeviceBean == null || DateUtils.compareTo
//                (deviceBean.getITEMDATE(), DateUtils.getStringTime(selectDeviceBean.getITEMDATE())) < 0) {
//            primaryDeviceMapper.insertRainfall(deviceBean);
//            logger.error("雨量数据插入成功->" + mDeviceBean.getCode());
//        }
//        for (int i=0;i<200;i++){
//            String addDay = addDay("2021-05-01", i);
//            System.out.println(addDay);
//        }

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date sDate = null;
        try {
            sDate = sdf.parse("2021-11-04");
        } catch (ParseException e) {
            e.printStackTrace();
        }
        long datePoorDay = DateUtils.getDatePoorDay(DateUtils.getNowDate(), sDate);

        System.out.println("day:"+datePoorDay+" addDay:"+ DateUtils.addDay("2021-11-04", 0));
        System.out.println("Date:"+getDate());
    }


}
